package cn.bugstack.domain.strategy.service.raffle;

import cn.bugstack.domain.strategy.model.entity.StrategyAwardEntity;
import cn.bugstack.domain.strategy.model.vo.RuleTreeVo;
import cn.bugstack.domain.strategy.model.vo.RuleWeightVO;
import cn.bugstack.domain.strategy.model.vo.StrategyAwardRuleModelVo;
import cn.bugstack.domain.strategy.model.vo.StrategyAwardStockKeyVo;
import cn.bugstack.domain.strategy.repository.IStrategyRepository;
import cn.bugstack.domain.strategy.service.AbstractRaffleStrategy;
import cn.bugstack.domain.strategy.service.IRaffleAward;
import cn.bugstack.domain.strategy.service.IRaffleRule;
import cn.bugstack.domain.strategy.service.IRaffleStock;
import cn.bugstack.domain.strategy.service.armory.IStrategyDispatch;
import cn.bugstack.domain.strategy.service.rule.chain.ILogicChain;
import cn.bugstack.domain.strategy.service.rule.chain.factory.DefaultChainFactory;
import cn.bugstack.domain.strategy.service.rule.tree.factory.DefaultTreeFactory;
import cn.bugstack.domain.strategy.service.rule.tree.factory.engine.IDecisionTreeEngine;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * @author 19872
 * 默认抽奖实现，继承抽象类
 */
@Service
@Slf4j
public class DefaultRaffleStrategy extends AbstractRaffleStrategy implements IRaffleAward, IRaffleStock, IRaffleRule {

    public DefaultRaffleStrategy(IStrategyRepository repository, IStrategyDispatch strategyDispatch, DefaultChainFactory defaultChainFactory, DefaultTreeFactory defaultTreeFactory) {
        super(repository, strategyDispatch, defaultChainFactory, defaultTreeFactory);
    }

    @Override
    public DefaultChainFactory.StrategyAwardVo raffleLogicChain(String userId, Long strategyId) {
        ILogicChain logicChain = defaultChainFactory.openLogicChain(strategyId);
        return logicChain.logic(userId, strategyId);
    }

    @Override
    public DefaultTreeFactory.StrategyAwardVO raffleLogicTree(String userId, Long strategyId, Integer awardId, Date endDateTime) {

        StrategyAwardRuleModelVo strategyAwardRuleModelVo = repository.queryStrategyAwardRuleValue(strategyId, awardId);

        if (null == strategyAwardRuleModelVo.getRuleModels()) {
            return DefaultTreeFactory.StrategyAwardVO.builder().build();
        }
        // 得到规则树对象
        RuleTreeVo ruleTreeVo = repository.queryRuleTreeVoByTreeId(strategyAwardRuleModelVo.getRuleModels());

        if (null == ruleTreeVo) {
            throw new RuntimeException("存在抽奖策略配置的规则模型 Key，未在库表rule_tree、rule_tree_node、rule_tree_line 配置对应的规则树信息");
        }

        IDecisionTreeEngine treeEngine = defaultTreeFactory.openLogicTree(ruleTreeVo);

        return treeEngine.process(userId, strategyId, awardId,endDateTime);
    }

    @Override
    public StrategyAwardStockKeyVo takeQueueValue() throws InterruptedException {

        return repository.takeQueueValue();
    }

    @Override
    public void updateStrategyAwardStock(Long strategyId, Integer awardId) {
        repository.updateStrategyAwardStock(strategyId, awardId);
    }

    @Override
    public List<StrategyAwardEntity> queryRaffleStrategyAwardList(Long strategyId) {
        return repository.queryStrategyAwardList(strategyId);
    }

    @Override
    public List<StrategyAwardEntity> queryRaffleStrategyAwardListByActivityId(Long activityId) {
        return repository.queryRaffleStrategyAwardListByActivityId(activityId);
    }

    @Override
    public Map<String, Integer> queryAwardRuleLockCount(String[] treeIds) {
        return repository.queryAwardRuleLockCount(treeIds);
    }

    @Override
    public List<RuleWeightVO> queryAwardRuleWeightByActivityId(Long activityId) {

        return repository.queryAwardRuleWeightByActivityId(activityId);
    }
}
